package com.org.controller.register;

import com.org.entity.RegistrationRequest;
import com.org.mapper.UserMapper;
import com.org.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 *@author Rainy-Heights
 *@version 1.0
 *@Date 2023/2/26 11:24
 */
@Controller
@RequestMapping("/register")
public class RegisterPageController {
    public String currentName=" ";

    public String getCurrentName() {
        return currentName;
    }

    @Resource
    UserMapper userMapper;

    @Autowired
    RegisterService registerService;

//     @RequestMapping("/auth-register")
//    public String register(){
//         return "/auth-register";
//    }
    @RequestMapping(value = "/auth-register")
    public String register(Model model){
        RegistrationRequest request=new RegistrationRequest();
        model.addAttribute("request",request);//添加视图层信息，引入静态资源文件后，就可以展示一个静态页面了，
        // 如果想让页面显示动态的数据，就需要页面从后端获取数据，使用变量表达式： ${ }
        return "/auth-register";
    }

    //直接跳转到下面的路由
    @RequestMapping(value = "/auth-register",method = RequestMethod.POST)
    public String regist(@ModelAttribute("request") RegistrationRequest request){
        registerService.register(request);
        System.out.println(request.getUsername());
        currentName=request.getUsername();
        System.out.println(request.getPassword());
        userMapper.registerUserByEveryParam(request.getUsername(), "user", request.getPassword());
        return "redirect:/auth-login";
    }
}
